const express = require('express')
const router = express.Router()
const houseService = require('services/house.js')
const settingService = require('services/setting')
const bookedDateService = require('services/bookedDate')
const filterService = require('services/filter')

/**
 * 列表
 */
router.get('/', async (req, res, next) => {
  try {
    res.json(await houseService.query(req.query, req.query.page))
  } catch (e) {
    next(e)
  }
})

router.get('/filters', async (req, res, next) => {
  try {
    const filters = await filterService.get()
    res.json(filters)  
  } catch (e) {
    next(e)
  }
})

/**
 * 精选
 */
router.post('/selected', async (req, res, next) => {
  try {
    res.json(await houseService.saveSelected(req.body.ids))
  } catch (e) {
    next(e)
  }
})

router.get('/selected', async (req, res, next) => {
  try {
    res.json(await houseService.getSelected())
  } catch (e) {
    next(e)
  }
})

/**
 * 已预订时段
 */
router.get('/:id/bookedDate', async (req, res, next) => {
  const {year, month} = req.query
  if (!year || !month) {
    return next(new BizError('missing year or month'))
  }
  const houseId = req.params.id
  let days = await bookedDateService.bookedDaysWithMonth(houseId, year, month)
  res.json(days)
})

router.get('/:id/closedDate', async (req, res, next) => {
  const {year, month} = req.query
  if (!year || !month) {
    return next(new BizError('missing year or month'))
  }
  const houseId = req.params.id
  let days = await bookedDateService.closedDaysWithMonth(houseId, year, month)
  res.json(days)
})

/**
 * 开放/关闭日期
 */
router.post('/:id/dateStatus', async (req, res, next) => {
  const houseId = req.params.id
  const { date, opened } = req.query
  const [ year, month, day] = date.split('/')
  try {
    if (+opened) {
      await bookedDateService.removeClosed(houseId, year, month, day)
    } else {
      await bookedDateService.addClosed(houseId, year, month, day)
    }
    res.json({status: 'success'})
  } catch (e) {
    next(e)
  }
})

/**
 * 增删改
 */
const processHouse = (house) => {
  ['usage', 'facility'].forEach((key) => {
    if (house[key]) {
      house[key] = house[key].join(',')
    }
  })
  if (house.pics.constructor === Array) {
    house.pics = JSON.stringify(house.pics)
  }
  return house
}

router.get('/:id', async (req, res) => {
  res.json(await houseService.get(req.params.id))
})

router.delete('/:id', async (req, res, next) => {
  try {
    await houseService.remove(req.params.id)
    res.json({status: 'ok'})
  } catch (e) {
    next(e)
  }
})

router.post('/', async (req, res, next) => {
  try {
    let house = processHouse(req.body)
    const { name, type } = house
    if (!name) {
      res.status(500).json({message: '请输入空间名'})
      return
    }
    if (!type) {
      res.status(500).json({message: '请选择空间类型'})
      return
    }
    const id = await houseService.create(house)
    res.json({id})
  } catch (e) {
    next(e)
  }
})

router.put('/:id', async (req, res, next) => {
  try {
    let house = processHouse(req.body)
    await houseService.update(req.params.id, house)
    res.json({status: 'ok'})
  } catch (e) {
    next(e)
  }
})

module.exports = router